# -*- coding: utf-8 -*-
# @Author  : pony.guan
# @Time    : 2021/12/27 14:59
# Description:


import sys
from datetime import datetime

from PyQt5.QtWidgets import QApplication, QMainWindow

from xy import Ui_MainWindow
import xy_api
from PyQt5.QtGui import QColor


class MyMainForm(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MyMainForm, self).__init__(parent)
        self.setupUi(self)
        # 设置tab颜色
        # self.xy.tabBar().setTabTextColor(0, QColor(0, 0, 255))
        # self.xy.tabBar().setTabTextColor(1, QColor(0, 255, 0))
        # self.xy.tabBar().setTabTextColor(2, QColor(255, 0, 0))
        # self.login_Button.clicked.connect(self.display)

        self.djqh.clicked.connect(self.switch)
        self.pushButton.clicked.connect(self.callback_re_push)
        self.pushButton_2.clicked.connect(self.container_move_out)
        self.pushButton_3.clicked.connect(self.get_repeat_container)

    def get_repeat_container(self):
        self.textBrowser_4.setText('')
        dt = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        self.textBrowser_4.append(f'执行时间: {dt}')
        self.textBrowser_4.append('\n')
        try:
            ret = xy_api.query_repeat_containers()
        except Exception as e:
            self.textBrowser_4.setText('api接口调用出错')
        else:
            for i in ret:
                desc, bins = i
                self.textBrowser_4.append(desc)
                self.textBrowser_4.append('\n'.join(bins))
                self.textBrowser_4.append('\n')

    def switch(self):
        tagCode = self.tagCode.currentText().strip()
        dt = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        try:
            ret = xy_api.switch(tagCode)
        except Exception as e:
            self.textBrowser.append(f'执行时间: {dt}')
            self.textBrowser.append('执行失败: ' + f'switch接口调用出错: str{e}' + '\n')
        else:
            self.textBrowser.append(f'执行时间: {dt}')
            self.textBrowser.append('执行结果: ' + ret + '\n')

    def callback_re_push(self):
        taskGroupCode = self.lineEdit.text().strip()
        taskCode = self.lineEdit_4.text().strip()
        containerCode = self.lineEdit_3.text().strip()
        locationCode = self.lineEdit_2.text().strip()

        if not all([taskGroupCode, taskCode, containerCode, locationCode]):
            self.textBrowser_2.setText('四个输入框都不能为空')
            return
        dt = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        try:
            ret = xy_api.callback_re_push(taskGroupCode, taskCode, containerCode, locationCode)
        except Exception as e:
            self.textBrowser.append(f'执行时间: {dt}')
            self.textBrowser.append('执行失败: ' + f'callback_re_push接口调用出错: {str(e)}' + '\n')
        else:
            self.textBrowser_2.append(f'执行时间: {dt}')
            self.textBrowser_2.append('执行结果: ' + ret + '\n')

    def container_move_out(self):

        containerCode = self.lineEdit_5.text().strip()

        positionCode = self.lineEdit_6.text().strip()

        if not all([containerCode, positionCode]):
            self.textBrowser_3.setText('料箱编号和位置编码都不能为空')
            return
        dt = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        try:
            ret = xy_api.container_move_out(containerCode, positionCode)
        except Exception as e:
            self.textBrowser.append(f'执行时间: {dt}')
            self.textBrowser.append('执行失败: ' + f'container_move_out接口调用出错: {str(e)}' + '\n')
        else:
            self.textBrowser_3.append(f'执行时间: {dt}')
            self.textBrowser_3.append('执行结果: ' + ret + '\n')


if __name__ == "__main__":
    app = QApplication(sys.argv)

    myWin = MyMainForm()

    myWin.show()

    sys.exit(app.exec_())
